package Demo04;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/*
计算器界面
 */
public class Calc  extends JFrame implements ActionListener { // extends (继承) JFrame（窗口类）
    //铺面板 ----桌布
    private JPanel mainPanel,topPanel,buttomPanel;//private 私有的***面板1主,上，下
    private  JTextField txtResult;


    //四行四列---排列
    //定义对象
    //按钮；
    private JButton btn0,btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8,btn9,btnJia,btnJian,btnCheng,btnChu,btnDian,btnDeng,btnC;
   //操作数
    private  double op1,op2,result;
    //操作符 +/-*
    private  char op;
    //初始化--实例化
    public Calc(){

        mainPanel=new JPanel(new FlowLayout());//边界布局
//        mainPanel=new JPanel(new BorTest());//流布局
        topPanel=new JPanel(new FlowLayout());//流布局
        buttomPanel =new JPanel(new FlowLayout());//表格布局

//        GridLayout layout=new GridLayout(4,4);//表格；
//        mainPanel=new JPanel(new GridLayout(4,4,2,2));//4行4列，行/列：间距为2
        buttomPanel=new JPanel(new GridLayout(4,4,2,2));
//        mainPanel.setLayout(layout);

        this.setContentPane(mainPanel);//铺；桌布
        //加面板
        mainPanel.add(topPanel,"North");
        mainPanel.add(buttomPanel,"Center");

        //文本框实例化；
        txtResult=new JTextField(20);
        txtResult.setFont(new Font("宋体",Font.BOLD,15));

        topPanel.add(txtResult);

//        txtResult.setFont("宋体",Font.BOLD,20);


        btn0=new MyButton("0");
        btn1=new MyButton("1");
        btn2=new MyButton("2");
        btn3=new MyButton("3");
        btn4=new MyButton("4");
        btn5=new MyButton("5");
        btn6=new MyButton("6");
        btn7=new MyButton("7");
        btn8=new MyButton("8");
        btn9=new MyButton("9");
        btnJia=new MyButton("+");
        btnJian=new MyButton("-");
        btnCheng=new MyButton("*");
        btnChu=new MyButton("/");
        btnDian=new MyButton(".");
        btnDeng=new MyButton("=");
        btnC=new JButton("C"); //增加按钮 ；


        topPanel.add(btnC);////增加按钮 ；
        //
        buttomPanel.add(btn1);
        buttomPanel.add(btn2);
        buttomPanel.add(btn3);
        buttomPanel.add(btnJia);
        buttomPanel.add(btn4);
        buttomPanel.add(btn5);
        buttomPanel.add(btn6);
        buttomPanel.add(btnJian);
        buttomPanel.add(btn7);
        buttomPanel.add(btn8);
        buttomPanel.add(btn9);
        buttomPanel.add(btnCheng);
        buttomPanel.add(btn0);
        buttomPanel.add(btnDian);
        buttomPanel.add(btnDeng);
        buttomPanel.add(btnChu);
//        mainPanel.add(btn0);


        //监听按钮 ；
        btn0.addActionListener(this);
        btn1.addActionListener(this);
        btn2.addActionListener(this);
        btn3.addActionListener(this);
        btn4.addActionListener(this);
        btn5.addActionListener(this);
        btn6.addActionListener(this);
        btn7.addActionListener(this);
        btn8.addActionListener(this);
        btn9.addActionListener(this);
        btnJian.addActionListener(this);
        btnJia.addActionListener(this);
        btnCheng.addActionListener(this);
        btnChu.addActionListener(this);
        btnDian.addActionListener(this);
        btnDeng.addActionListener(this);
        btnC.addActionListener(this);
//        btn0.addActionListener(this::actionPerformed);
//        btn0.addActionListener(this::actionPerformed);
//        btnC.addActionListener(this);

        //窗口设置；
        this.setBounds(200,200,250,255);//窗口的位置大小 ；
        this.setVisible(true);//看得见
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);//设置关闭之后不运行；
        this.setResizable(false);////不可改变大小
    }

    //主方法；
    public static void main(String[] args) {

        new Calc();
    }

    @Override//重载；
    public void actionPerformed(ActionEvent e) {
        //数字1~9；
        for (int i=1;i<=9;i++){
            if (e.getActionCommand().equals(String.valueOf(i))){
                txtResult.setText(txtResult.getText()+i);
            }
        }

        if (e.getSource().equals(btnC)){
            //清空；
            txtResult.setText("");
        }

        //0
        if (e.getSource().equals(btn0)){
            double n= 0;
            try{
                n=Double.valueOf(txtResult.getText()) ;
            }catch (Exception e2){
                txtResult.setText("0");
            }
//            if (txtResult.getText())

            if (n!=0){
                txtResult.setText(txtResult.getText()+"0");

            }
        }

        //小数点；
        if (e.getSource().equals(btnDian)){
            String str=txtResult.getText();
            if (str.indexOf('.')<0){
                txtResult.setText(txtResult.getText()+".");

            }
        }

        //+-*/
        if (e.getSource().equals(btnJia)){
            op='+';
            op1=Double.valueOf(txtResult.getText());
            txtResult.setText("");
        }
        if (e.getSource().equals(btnJian)){
            op='-';
            op1=Double.valueOf(txtResult.getText());
            txtResult.setText("");
        }
        if (e.getSource().equals(btnCheng)){
            op='*';
            op1=Double.valueOf(txtResult.getText());
            txtResult.setText("");
        }
        if (e.getSource().equals(btnChu)){
            op='/';
            op1=Double.valueOf(txtResult.getText());
            txtResult.setText("");
        }


        //结果=
        if (e.getSource().equals(btnDeng)){
            op2=Double.valueOf(txtResult.getText());
            switch (op){
                case '+':
                    result=op1+op2;
                    break;
                case '-':
                    result=op1-op2;
                    break;
                case '*':
                    result=op1*op2;
                    break;
                case '/':
                    result=op1/op2;
                    break;

            }
            txtResult.setText(String.valueOf(result));
        }




        /*
        if (e.getSource().equals(btn0)){
            txtResult.setText(txtResult.getText()+"0");
        }
        if (e.getSource().equals(btn1)){
            txtResult.setText(txtResult.getText()+"1");
        }
        if (e.getSource().equals(btn2)){
            txtResult.setText(txtResult.getText()+"2");
        }
        if (e.getSource().equals(btn3)){
            txtResult.setText(txtResult.getText()+"3");
        }
        if (e.getSource().equals(btn4)){
            txtResult.setText(txtResult.getText()+"4");
        }
        if (e.getSource().equals(btn5)){
            txtResult.setText(txtResult.getText()+"5");
        }
        if (e.getSource().equals(btn6)){
            txtResult.setText(txtResult.getText()+"6");
        }
        if (e.getSource().equals(btn7)){
            txtResult.setText(txtResult.getText()+"7");
        }
        if (e.getSource().equals(btn8)){
            txtResult.setText(txtResult.getText()+"8");
        }
        if (e.getSource().equals(btn9)){
            txtResult.setText(txtResult.getText()+"9");
        }
        if (e.getSource().equals(btnJia)){
            txtResult.setText(txtResult.getText()+"+");
        }
        if (e.getSource().equals(btnJian)){
            txtResult.setText(txtResult.getText()+"-");
        }
        if (e.getSource().equals(btnCheng)){
            txtResult.setText(txtResult.getText()+"*");
        }
        if (e.getSource().equals(btnChu)){
            txtResult.setText(txtResult.getText()+"/");
        }
        if (e.getSource().equals(btnDian)){
            txtResult.setText(txtResult.getText()+".");
        }
        if (e.getSource().equals(btnDeng)){
            txtResult.setText(txtResult.getText()+"=");
        }
        if (e.getSource().equals(btnC)){
            txtResult.setText(txtResult.getText(""));
        }

         */




    }

    /*
    边界部局 ：东南西北中
     */
}



































